var models = require('../models/user.js');
var lession = require('../models/lession.js').lession;
var type = require('../models/lessiontype.js').lessiontype;
var comment = require('../models/comment.js').comment;
var ObjectId = require('mongoose').Types.ObjectId;
var moment = require('moment');
function executData(user,id){
	user.forEach(function(item){
		item.create_time = moment(item.create_time).format("YYYY-MM-DD HH:mm");
		var arr = item.user;
		item.isMy = false;
		item.user = [];
		if(arr&&arr.indexOf(id)>-1){
			item.isMy = true;
		}
	});
}
module.exports.autoroute = {
	get:{
		'/lession/list':function(req,res){
			var page = 1;
			if(req.query.page)
				page = req.query.page;
			condition = {};
			if(req.query.typeid)
				condition.typeid = ObjectId(req.query.typeid);
			lession.find(condition).sort('-create_time').skip((page-1)*10).limit(10).lean(true).exec(function(err,user){
				executData(user,req.session.user._id)
	  			res.json({code:1,message:user});
			});
		},
		'/type/list':function(req,res){
			var page = 1;
			if(req.query.page)
				page = req.query.page;
			type.find({}).skip((page-1)*10).limit(10).lean(true).exec(function(err,user){
				executData(user,req.session.user._id)
	  			res.json({code:1,message:user});
			});
		},
		'/lession/search':function(req,res){
			var page = 1;
			if(req.query.page)
				page = req.query.page;
			condition = {};
			if(req.query.name)
				condition.lessionname = new RegExp(req.query.name);
			lession.find(condition).skip((page-1)*10).limit(10).lean(true).exec(function(err,user){
				executData(user,req.session.user._id)
	  			res.json({code:1,message:user});
			});
		},
		'/lession/comment':function(req,res){
			var page = 1;
			if(req.query.page)
				page = req.query.page;
			condition = {};
			if(req.query.lessionid)
				condition.lessionid = ObjectId(req.query.lessionid);
			comment.find(condition).sort('-create_time').skip((page-1)*10).limit(10).lean(true).exec(function(err,user){
				executData(user)
	  			res.json({code:1,message:user});
			});
		},
		'/lession/addMy':function(req,res){
			lession.findByIdAndUpdate(req.query.lessionid,{'$push':{'user':req.session.user._id}},function(err,user){
				models.user.findByIdAndUpdate(req.session.user._id,{'$push':{'lession':req.query.lessionid}},function(err,user){
			    	res.send({code:1,message:"操作成功！"});
				});
			});
		},
		'/lession/removeMy':function(req,res){
			lession.findByIdAndUpdate(req.query.lessionid,{'$pop':{'user':req.session.user._id}},function(err,user){
				models.user.findByIdAndUpdate(req.session.user._id,{'$pop':{'lession':req.query.lessionid}},function(err,user){
				    res.send({code:1,message:"操作成功！"});
				});
			});
		},
		'/comment/up':function(req,res){
			comment.findByIdAndUpdate(ObjectId(req.query.id),{'$inc':{'up':1}},function(err,user){
			    res.send({code:1,message:"操作成功！"});
			});
		},
		'/comment/down':function(req,res){
			comment.findByIdAndUpdate(ObjectId(req.query.id),{'$inc':{'down':1}},function(err,user){
			    res.send({code:1,message:"操作成功！"});
			});
		}
	},
	post:{
		'/lession/comment':function(req,res){
			var callback = function(err){
			  if(err){
			    debug(err);
			    res.send({code:-1,message:'保存失败'})
			  }else
			    res.send({code:1,message:'保存成功'})
			};

			var _comment = new comment({
			  content : req.body.content, 
			  lessionid : ObjectId(req.body.lessionid),
			  fromUser : {userid:req.session.user._id,username:req.session.user.username},
			  up:0,
			  down:0
			});
			console.log(_comment)
			_comment.save(callback);
		}
	}
};

